package com.example.finallyjob.service;

import com.example.finallyjob.dao.IndexImp;
import com.example.finallyjob.entity.Article;
import com.example.finallyjob.entity.User;
import com.example.finallyjob.mapper.IndexMapper;
import com.example.finallyjob.tool.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class IndexService {
    Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private IndexImp indexImp;
    @Autowired
    private MongoTemplate mongoTemplate;


    public List<Article> NewText(Integer page,Integer size) {
        List<Article> articles = indexImp.NewArticle("text");
        Page<Article> page1=new Page<>(page,size,articles);
        articles= page1.getArticles();
        return articles;
    }


    public List<Article> NewVideo(Integer page,Integer size) {
        List<Article> articles = indexImp.NewArticle("video");
        Page<Article> page1=new Page<>(page,size,articles);
        articles= page1.getArticles();
        return articles;
    }

    public List<Article> HotText(Integer page,Integer size){
        List<Article> articles=indexImp.HotArticle("text");
        Page<Article> page1=new Page<>(page,size,articles);
        articles= page1.getArticles();
        return articles;
    }


    public List<Article>  HotVideo(int page,int size){
        List<Article> articles=indexImp.HotArticle("video");
        Page<Article> page1=new Page<>(page,size,articles);
        articles= page1.getArticles();
        return articles;
    }


    public List<Article>  selectT(String name, int page, Integer size) throws ServletException, IOException {
        Article article=new Article();
        article.setTitle(name);
        List<Article> tproducts=indexImp.selectTitle(article);
        Page<Article> articlePage=new Page<>(page,size,tproducts);
        return articlePage.getArticles();
    }


    public List<User>  selectU(String name,Integer page,Integer size){
        User user=new User();
        user.setUserName(name);
        List<User> products=indexImp.selectU(user);
        Page<User> userPage=new Page<>(page,size,products);
        return userPage.getArticles();
    }


    public List<Article> TextZone(String type,String kind,int page,Integer size){
        Article article=new Article();
        article.setType(type);
        article.setKind(kind);
        List<Article> articles=indexImp.Zone(article);
        Page<Article> page1=new Page<>(page,size,articles);
        articles= page1.getArticles();
        return articles;
    }

    @Transactional(readOnly = true)
    public User userDetail(Integer id) throws Exception {
        User user=new User();
        user.setUserId(id);
        user=indexImp.selectById(user);
        if(user.getUserName() != null) {
            User user1 = mongoTemplate.findById(id, User.class);
            if(user1 != null)
            {
                user.setVideoId(user1.getVideoId());
                user.setArticleId(user1.getArticleId());
                user.setFriend(user1.getFriend());
            }
        }else{
            throw new Exception();
        }
        return user;
    }
}